from apps.merchant.genera.baseViews import *


class GeneraTreeViewSet(BaseGeneraViewSet):
    authentication_classes = []
    permission_classes = []
    def get_queryset(self):
        params = getRequestParams(self.request)
        _lists = GeneraDao().findAll()
        parentId = params.get('parentId')
        if parentId:
            _lists = _lists.filter(parent=parentId)
        else:
            _lists = _lists.filter(parent__isnull=True)
        return _lists
    
class GeneraViewSet(BaseGeneraViewSet):
    authentication_classes = []
    permission_classes = []
    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = GeneraDao().findAll()
        return _list.order_by('id')
    
    @action(methods=['get'], detail=False)
    def getShopGenera(self, request):
        _generas = GeneraDao().findAll().filter(parent__isnull=True).order_by('id')
        data = GeneraModelSerializer(_generas, many=True).data

        return Response(response_success({'list': [{'name': '推荐', 'key': 'recommend'}] + data}))
